<template>
  <el-dialog title="新增员工" :visible.sync="showDialog" @close="closeHandler">
    <!-- 表单 -->
    <el-form ref="employeeRef" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width: 80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width: 80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width: 80%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width: 80%" placeholder="请选择">
          <el-option v-for="item in hireTypes" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width: 80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width: 80%" placeholder="请选择部门" @focus="getDepartments" />
        <!-- 放置一个tree组件 -->
        <el-tree v-if="showTree" v-loading="loading" :data="treeData" default-expand-all :props="defaultProps" @node-click="selectDepartment" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <!-- 时间日期控件 -->
        <el-date-picker v-model="formData.correctionTime" style="width: 80%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="closeHandler">取消</el-button>
          <el-button type="primary" size="small" @click="submit">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
// 导入常量中员工常量
import employees from '@/api/constant/employees'
import { getDepartments } from '@/api/departments'
import { addEmployeeApi } from '@/api/employees'
// 导入公共的工具函数
import { _noLimitCate } from '@/utils'
export default {
  props: {
    // 员工列表数据
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    // 手机号码的唯一性验证
    const checkMobileRepeat = (rules, value, callback) => {
      // 添加时
      const isRepeat = this.$props.list.some(item => item.mobile === value)
      isRepeat ? callback(new Error('手机号码重复')) : callback()
    }
    // 工号的唯一性验证
    const checkWorkNumberRepeat = (rules, value, callback) => {
      // 添加时
      const isRepeat = this.$props.list.some(item => item.workNumber === value)
      isRepeat ? callback(new Error('工号重复')) : callback()
    }
    return {
      // 聘用形式
      hireTypes: employees.hireType,
      showDialog: false,
      // 表单数据
      formData: {
        username: '', // 姓名
        mobile: '', // 手机号码
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 4,
            message: '用户姓名为1-4位',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          },
          {
            validator: checkMobileRepeat,
            trigger: 'blur'
          }
        ],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' },
          {
            pattern: /^[1-9]\d*$/,
            message: '工号必须是数字',
            trigger: 'blur'
          },
          {
            validator: checkWorkNumberRepeat,
            trigger: 'blur'
          }
        ],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      // 控制部门树形组件的显示和隐藏
      showTree: false,
      // 树形组件的数据
      treeData: [],
      // 树形组件的控制
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 控制数据请求进度条
      loading: false
    }
  },
  methods: {
    // 自定义open方法来控制对话框的显示
    open() {
      this.showDialog = true
    },
    // 关闭对话框处理函数
    closeHandler() {
      this.showDialog = false
      this.$refs.employeeRef.resetFields()
    },
    // 提交表单数据
    submit() {
      this.$refs.employeeRef.validate(async isOk => {
        // 验证失败
        if (!isOk) return false
        // 验证通过
        // 添加
        await addEmployeeApi(this.formData)
        this.$message.success('添加员工成功')
        // 关闭对话框
        this.showDialog = false
        // 触发父组件中重新获取最新员工列表数据的自定义事件
        // this.$emit('get-new-employees')
        // 直接调用父组件的方法
        this.$parent.getEmployeeList()
      })
    },
    // 部门数据的文本输入框获取焦点事件
    async getDepartments() {
      // 显示树形组件
      this.showTree = true
      this.loading = true
      // 获取部门数据
      const { depts } = await getDepartments()
      // 给部门数据赋值并处理成树状数据
      this.treeData = _noLimitCate(depts, '')
      this.loading = false
    },
    // 选中树形节点
    selectDepartment(node) {
      // 给文本输入框赋值
      this.formData.departmentName = node.name
      // 关闭树形组件
      this.showTree = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
